package com.lanou.zhihu.web.controller;

import com.lanou.zhihu.entity.User;
import com.lanou.zhihu.service.UserService;
import com.lanou.zhihu.utils.MD5;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by QuChao on 2018/10/24.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/getSession")
    public User getSession(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            user.setPassword("");
            return user;
        } else {
            return null;
        }
    }

    @ResponseBody
    @RequestMapping("/findById")
    public User findById(Integer id) {
        User user = userService.findById(id);
        user.setPassword("");
        return user;
    }

    @ResponseBody//查询用户所有信息
    @RequestMapping("/personal_information")
    public Map<String, Object> personal_information(HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<>();
        User user = (User) request.getSession().getAttribute("user");
        User user2 = userService.register1(user.getId());
        map.put("msg", user2);
        System.out.println(map);
        return map;
    }

    @ResponseBody
    @RequestMapping("/login")//登录
    public Map<String, Object> login(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password1 = request.getParameter("password");
        String s = MD5.MD5(password1+"-/`");
        User user = userService.login(username, s);
        Map<String, Object> map = new HashMap<>();
        if (user != null) {
            map.put("msg", "success");
            request.getSession().setAttribute("user", user);
            return map;
        } else {
            map.put("msg", "out");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping("/changePassword")//修改密码
    public Map<String, Object> changePassword(String username, String phone, String password) {
        int id = userService.change(username, phone);
        String s = MD5.MD5(password+"-/`");
        boolean t = userService.changepass(id, s);
        Map<String, Object> map = new HashMap<>();
        if (t == true) {
            map.put("message", "success");
            return map;
        } else {
            map.put("message", "error");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping("/regist")//判断数据库是否有此账号
    public String regist(User user) {
        User user1 = userService.personal_information(user.getUsername());
        if (user1 != null) {
            return "error";
        } else {
            return "success";
        }
    }

    @ResponseBody
    @RequestMapping("/register")//注册
    public Map<String,Object> register(User user) {
        String username = user.getUsername();
        String s = MD5.MD5(user.getPassword()+"-/`");
        System.out.println(s);
        User user1 = userService.personal_information(username);
        Map<String,Object> map = new HashMap<>();
        if (user1 != null) {
            map.put("msg", "error");
            System.out.println("用户名已存在");
            return map;
        } else {
            userService.register(username, s, user.getPhone());
            System.out.println("用户名: "+username+" 密码"+s +"  手机号:"+user.getPhone());
            map.put("msg", "success");
            return map;
        }

    }

    //注销
    @ResponseBody
    @RequestMapping("/loginOutput")
    public boolean loginOutput(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("modifyThePicture")
    public void modifyThePicture(HttpServletRequest request, String handimage, Part img, HttpServletResponse response) throws IOException {
        InputStream is = img.getInputStream();//获取输入流
        String fileName = new Date().getTime() + ".jpg";
        Client client = Client.create();//存到其他服务器
        String filePath = "http://47.106.73.53/zhihu/UserImage/" + fileName;
        WebResource resource = client.resource(filePath);//服务器地址
        resource.put(is);//存入文件
        User user = (User) request.getSession().getAttribute("user");
        userService.modifyThePicture(user.getId(), filePath);
        System.out.println(user);
        response.getWriter().print(filePath);
    }
}
